<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<!--标记面板-->
<div class="modal fade" id="modal-mark-panel">
    <div class="modal-dialog" style="width: 550px;">
        <div class="modal-content">
            <div class="modal-header" style="padding-bottom: 0px;">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">项目划库存标记</h4>
            </div>
            <div class="modal-body" style="padding-bottom: 5px;padding-top: 0px;">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="box">
                            <div class="box-header" style="padding-bottom: 0px;">

                            </div>
                            <div class="box-body">
                                <form id="modalForm" class="form-horizontal">
                                    <div class="row">
                                        <input type="hidden" name="pimBeforeItyId">
                                        <input type="hidden" name="pimItyType">
                                        <div class="form-group" style="height: 60px">
                                            <label style="width: 90px;text-align: right">
                                                物料描述
                                            </label>
                                            <label>
                                                <input type="text" id="pimMatDesc" name="pimMatDesc"
                                                       class="form-control" readonly="readonly" style="width: 350px"/>
                                            </label>

                                        </div>
                                        <div class="form-group" style="height: 60px">
                                            <label style="width: 90px;text-align: right">SAP物料编码</label>
                                            <label>
                                                <input type="text" id="pimSapCode" name="pimSapCode"
                                                       class="form-control" readonly="readonly" style="width: 350px"/>
                                            </label>
                                        </div>
                                        <div class="form-group" style="height: 60px">
                                            <label style="width: 90px;text-align: right">项目编码</label>
                                            <label>
                                                <input type="text" id="pimBeforeProCode" name="pimBeforeProCode"
                                                       class="form-control"
                                                       readonly="readonly" style="width: 350px"/>
                                            </label>
                                        </div>
                                        <div class="form-group" style="height: 60px">
                                            <label style="width: 90px;text-align: right">项目名称</label>
                                            <label>
                                                <input type="text" id="pimBeforeProName" name="pimBeforeProName"
                                                       class="form-control"
                                                       readonly="readonly" style="width: 350px"/>
                                            </label>
                                        </div>
                                        <div class="form-group" style="height: 60px">
                                            <label style="width: 90px;text-align: right">可用数量</label>
                                            <label>
                                                <input type="text" id="pimBeforeAvaNum" name="pimBeforeAvaNum"
                                                       class="form-control"
                                                       readonly="readonly" style="width: 100px"/>
                                            </label>
                                            <label style="width: 90px;text-align: right; margin-left: 50px">可用重量</label>
                                            <label>
                                                <input type="text" id="pimBeforeAvaWeight" name="pimBeforeAvaWeight"
                                                       class="form-control"
                                                       readonly="readonly" style="width: 100px"/>
                                            </label>
                                        </div>
                                        <div class="form-group" style="height: 60px">
                                            <label style="width: 90px;text-align: right">
                                                标记数量
                                            </label>
                                            <label>
                                                <input type="text" id="pimNum" name="pimNum"
                                                       class="form-control"
                                                       placeholder="请输入标记数量" oninput="checkValue(this)"
                                                       style="width: 100px">
                                            </label>
                                            <label style="width: 90px;text-align: right; margin-left: 50px">标记重量</label>
                                            <label>
                                                <input type="text" id="pimWeight" name="pimWeight"
                                                       class="form-control"
                                                       readonly="readonly" style="width: 100px"/>
                                            </label>
                                        </div>
                                        <div class="form-group" style="height: 60px">
                                            <label style="width: 90px;text-align: right">
                                                划拨说明
                                            </label>
                                            <label>
                                                <input type="text" id="pimRemark" name="pimRemark"
                                                       class="form-control"
                                                       placeholder="请输入划拨说明"
                                                       style="width: 350px">
                                            </label>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer" style="padding-top: 5px;">
                <button type="button" class="btn btn-info" id="btn-mark-commit">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>

</div>
<section class="content-header">
    <h1 style="display: inline-block" id="title">项目划库存申请</h1>
    <button onclick="javascript:window.history.back();" type="button" class="btn btn-success"
            style="text-align:center;vertical-align:middle;font-size:30px;position:absolute;right:3%;display: inline-block;background-color: transparent;border: none;color: #5c5c5c;outline:none;">
        <i style="-moz-transform:scaleX(-1);-webkit-transform:scaleX(-1);-o-transform:scaleX(-1);transform:scaleX(-1);filter:FlipH;"
           class="glyphicon glyphicon-share-alt"></i><span
            style="margin-left: 5px;font-size: 16px;position: absolute;top: 20%;">返回</span>
    </button>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <div class="row">
                        <div class="form-inline">
                            <form id="queryForm">
                                <div class="form-group">
                                    <label>
                                        物料描述
                                    </label>

                                    <input name="ityMatDesc" type="text" class="form-control text" maxlength="255"
                                           placeholder="请输入物料描述"/>

                                </div>
                                <div class="form-group">
                                    <label>
                                        SAP物料编码
                                    </label>

                                    <input name="itySapCode" type="text" class="form-control text"
                                           placeholder="请输入SAP物料编码"/>

                                </div>

                                <div class="form-group">
                                    <label>
                                        项目编号
                                    </label>

                                    <input name="ityProCode" type="text" class="form-control text" maxlength="255"
                                           placeholder="请输入项目编号"/>

                                </div>
                                <div class="form-group">
                                    <label>
                                        项目名称
                                    </label>

                                    <input name="ityProName" type="text" class="form-control text" maxlength="255"
                                           placeholder="请输入项目名称"/>

                                </div>
                                <div class="form-group">
                                    <label>
                                        剩余金额小于
                                    </label>

                                    <input name="totalAmount" type="text" class="form-control text" maxlength="255"
                                           placeholder="请输入金额" oninput="xyh.inputDecimalThree(this)"/>

                                </div>
                                <div class="form-group">
                                    <label>生产状态</label>
                                    <select name="ityProStatus" class="form-control select">
                                        <option value="" selected="selected">全部</option>
                                        <option value="-1">无状态</option>
                                        <option value="1">未开始</option>
                                        <option value="2">执行中</option>
                                        <option value="3">已完成</option>
                                        <option value="4">取消</option>
                                        <option value="5">暂缓</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>
                                        物料分类
                                    </label>
                                    <select name="ityMatType" id="ityMatType" class="form-control select">
                                        <option value="">全部</option>
                                        <option value="2">外协件</option>
                                        <option value="4">铝料</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <button type="button" class="btn btn-success" id="btn-search">搜索
                                    </button>
                                </div>
                            </form>
                        </div>
                    </div>

                </div>
                <div id="toolbar">
                    <button id="btn_markInventorys" type="button" class="btn btn-info">
                        批量标记
                    </button>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table" class="table table-bordered " width="100%">
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var table;

    /**
     *@FunctionName: showMarkPanel
     *@Description: 显示标记模态框
     *@Author: 徐一贺
     *@CreateDate: 2020/5/8 9:25
     *@Phone: 18241927380
     *@Param: [ityId] 库存表主键 [ityType] [库存类型]
     *@Version: 1.0.0
     */
    function showMarkPanel(ityId, ityType) {
        $.post(ctx + 'purchase/proInv/apply/getInventory', {
            pimBeforeItyId: ityId,
            pimItyType: ityType
        }, function (result) {
            if (result.type == web_status.SUCCESS) {
                $("#modalForm").fillData(result.data);
                js.table.refresh(table);
                js.modal.open("modal-mark-panel");
            } else if (result.type == web_status.FAIL) {
                js.modal.fail(result.msg);
            }
        }, 'json');
        js.modal.open();
    }

    /**
     *@FunctionName: checkValue
     *@Description: 修改标记数量  同时改变标记重量
     *@Author: TH
     */
    function checkValue(obj) {
        var weight = $("#modalForm [name='pimBeforeAvaWeight']").val();
        var avaNum = $("#modalForm [name='pimBeforeAvaNum']").val();
        var num = $("#modalForm [name='pimNum']").val();
        if (weight != null) {
            var pimWeight = weight / avaNum * num;
            pimWeight = Math.round(pimWeight * 1000) / 1000;
            $("#modalForm [name='pimWeight']").val(pimWeight);
        }
    }

    /**
     *@FunctionName: search
     *@Description: 查询
     *@Author: 徐一贺
     *@CreateDate: 2020/4/10 16:10
     *@Phone: 18241927380
     *@Version: 1.0.0
     */
    function search() {
        js.table.search(table);
    }

    /**
     *@FunctionName: selectAll
     *@Description: 全选
     *@Author: 徐一贺
     *@CreateDate: 2020/4/8 15:20
     *@Phone: 18241927380
     *@Version: 1.0.0
     */
    function selectAll(obj) {
        $('input[name="cb_change"]').each(function () {
            if (!$(this).prop("disabled")) {
                if ($(obj).prop("checked")) {
                    $(this).prop("checked", true);
                }
                if ($(obj).prop("checked") == false) {
                    $(this).prop("checked", false);
                }
            }
        })
    }

    $(function () {
        //加载列表数据
        table = js.table.init({
            id: 'bootstrap-table',
            url: ctx + "half/halInvManage/proToInvMark/inventoryList",
            showColumns: true,
            showExport: false,
            pageSize: 300,
            striped: false,
            uniqueId: "ityId",
            onLoadSuccess: function () {
                $('#inventoryQueryTable thead th').each(function () {
                    $(this).width(Math.floor($(this).width()));
                });
                $('#inventoryQueryTable tbody tr:first td').each(function () {
                    $(this).width(Math.floor($(this).width()));
                });
            },
            columns: [
                {
                    title: '   <input title="" id="selectAll" onclick="selectAll(this)" type="checkbox" />',
                    width: '80',
                    field: 'ityId',
                    formatter: function (value, row, index) {
                        return '<input  style="width: 30px;height: 30px"  id="cb_change' + row.ityId + '" name="cb_change" data="' + row.ityId + '" data-type="' + row.ityType + '"  type="checkbox" />';
                    }
                },
                {
                    title: '序号', field: 'pimId', width: '40',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: '生产状态', field: 'ityProStatus'},
                {title: '项目剩余总金额<BR>（万元）', field: 'totalAmount'},
                {
                    title: '物料分类', field: 'ityMatType',
                    formatter: function (value, row, index) {
                        if (value == 2) {
                            return "外协件";
                        } else if (value == 3) {
                            return "制作件";
                        } else if (value == 4) {
                            return "铝料";
                        } else {
                            return value;
                        }
                    }
                },
                {title: '物料描述', field: 'ityMatName'},
                {title: 'SAP物料编码', field: 'itySapCode'},
                {title: '可用数量', field: 'ityAvaNum'},
                {title: '单位', field: 'ityUnit'},
                {
                    title: '可用重量', field: 'ityAvaWeight', formatter: function (value, row, index) {
                        return Math.round(value * 1000) / 1000;
                    }
                },
                {title: '项目编号', field: 'ityProCode'},
                {title: '项目名称', field: 'ityProName'},
                {
                    title: '仓库类型', field: 'ityType', formatter: function (value, row, index) {
                        if (value == 'h') {
                            return '成品库';
                        } else if (value == 'p') {
                            return '外购件库';
                        } else if (value == 'm') {
                            return '原料库';
                        }
                    }
                },
                {
                    title: '操作', field: 'ityId',
                    formatter: function (value, row, index) {
                        var htmlText = '<button class="btn btn-info btn-xs" onclick="showMarkPanel(' + row.ityId + ',\'' + row.ityType + '\')">标记</button>';
                        return htmlText;
                    }
                }
            ]
        });
        /**
         * 表格搜索
         */
        $("#btn-search").on('click', function () {
            js.table.search(table);
        });

        /**
         * 标记确定
         * */
        $("#btn-mark-commit").on('click', function () {
            var pimNum = parseFloat($("#modalForm [name='pimNum']").val());
            var pimBeforeAvaNum = parseFloat($("#modalForm [name='pimBeforeAvaNum']").val());
            if (pimNum <= 0) {
                js.modal.warning("标记数量必须大于0！");
                return;
            }
            if (pimNum > pimBeforeAvaNum) {
                js.modal.warning("标记数量不能大于可用数量！");
                return;
            }
            js.submit({
                url: ctx + "half/halInvManage/proToInvMark/mark",
                data:
                    new FormData($("#modalForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.modal.hide("modal-mark-panel");
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            });
        });

        /**
         * 批量标记
         */
        $("#btn_markInventorys").on('click', function () {
            var ityObjArr = new Array();
            $('input[name="cb_change"]').each(function () {
                if (!$(this).prop("disabled")) {
                    if ($(this).prop("checked")) {
                        var ityObj = {
                            pimBeforeItyId: $(this).attr("data"),
                            pimItyType: $(this).attr("data-type")
                        };
                        ityObjArr.push(ityObj);
                    }
                }
            })
            if (ityObjArr.length == 0) {
                js.modal.warning("请选择要批量标记的库存!");
            }
            js.modal.confirm("您确认要标记这些库存吗？", function () {
                js.post({
                    url: ctx + 'purchase/proInv/apply/marks',
                    data: JSON.stringify(ityObjArr),
                    type: "post",
                    cache: false,
                    dataType: "json",
                    contentType: "application/json",
                    crossDomain: true == !(document.all),
                    success: function (res) {
                        if (res.type == web_status.SUCCESS) {
                            js.modal.success(res.msg);
                            js.table.refresh(table);
                            js.modal.closeLoading();
                        } else {
                            js.modal.warning(res.msg);
                            js.modal.closeLoading();
                        }
                    }
                });
            });
        });

        // 验证初始化
        js.validate.init("modalForm", {
            fields: {
                /*pimNum: {
                    validators: {
                        notEmpty: {message: '请输入标记数量'},
                    }
                },
                pimRemark: {validators: {notEmpty: {message: '请输入划拨说明'}}},*/
            }
        });
    });
</script>
</body>
</html>
